﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        private MasterViewModel mViewModel = null;
        public MainWindow()
        {
            InitializeComponent();

            if (null == mViewModel)
            {
                mViewModel = MasterViewModel.GetInstance();
                this.DataContext = mViewModel;
            }
        }

        private void OnCanExecuteSaveCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteSaveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructuresViewModel _structures = WorkBreakdownStructuresViewModel.GetInstance();
            EnvironmentalProfilesViewModel _profiles = EnvironmentalProfilesViewModel.GetInstance();
            ComponentsViewModel _components = ComponentsViewModel.GetInstance();
            if (null != _structures && null != _profiles)
            {
                Microsoft.Win32.SaveFileDialog _dialog = new Microsoft.Win32.SaveFileDialog();
                _dialog.DefaultExt = ".lcax";
                _dialog.Filter = "Life Cycle Assessment(*.lcax)|*.lcax|모든 파일|*.*";
                _dialog.Title = "LCA 모델링 저장하기";
                _dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (true == _dialog.ShowDialog())
                {
                    XmlDocument _doc = new XmlDocument();
                    XmlElement _root = _doc.CreateElement("OpenGreenerway");
                    XmlElement _profile = _profiles.Export(_doc);
                    XmlElement _structure = _structures.Export(_doc);
                    XmlElement _component = _components.Export(_doc);
                    if (null != _profile) _root.AppendChild(_profile);
                    if (null != _structure) _root.AppendChild(_structure);
                    if (null != _component) _root.AppendChild(_component);
                    _doc.AppendChild(_root);
                    _doc.Save(_dialog.FileName);
                }
            }
        }

        private void OnCanExecuteLoadCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteLoadCommand(object sender, ExecutedRoutedEventArgs e)
        {
            WorkBreakdownStructuresViewModel _structures = WorkBreakdownStructuresViewModel.GetInstance();
            EnvironmentalProfilesViewModel _profiles = EnvironmentalProfilesViewModel.GetInstance();
            ComponentsViewModel _components = ComponentsViewModel.GetInstance();
            if (null != _structures && null != _profiles)
            {
                Microsoft.Win32.OpenFileDialog _dialog = new Microsoft.Win32.OpenFileDialog();
                _dialog.DefaultExt = ".lcax";
                _dialog.Filter = "Life Cycle Assessment(*.lcax)|*.lcax|모든 파일|*.*";
                _dialog.Title = "LCA 모델링 불러오기";
                _dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (true == _dialog.ShowDialog())
                {
                    XmlDocument _doc = new XmlDocument();
                    _doc.Load(_dialog.FileName);
                    if (_doc.HasChildNodes)
                    {
                        XmlElement _root = _doc.FirstChild as XmlElement;
                        if (null == _root) return;
                        if ("OpenGreenerway" == _root.Name)
                        {
                            if (_root.HasChildNodes)
                            {
                                XmlElement _reserved = null;
                                int _index = 0;
                                ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
                                foreach (XmlNode _node in _root.ChildNodes)
                                {
                                    switch (_node.Name)
                                    {
                                        case "Groups":
                                            //  Object만 나중에 해야하기 때문에 아래 코드를 주석 처리하고 나눠 처리로 진행
                                            //_structures.Import(_node as XmlElement);
                                            if (_node.HasChildNodes)
                                            {
                                                for (int i = 0; i < _node.ChildNodes.Count; i++)
                                                {
                                                    XmlElement _child = _node.ChildNodes[i] as XmlElement;
                                                    if ("WorkBreakdownStructureGroup" == _child.Name)
                                                    {
                                                        if (_configuration.Object == _child.Attributes["Name"].Value)
                                                        {
                                                            _reserved = _child;
                                                            _index = i;
                                                        }
                                                        else
                                                        {
                                                            WorkBreakdownStructureGroupViewModel _group = new WorkBreakdownStructureGroupViewModel();
                                                            _group.Import(_child);
                                                            _structures.Groups.Add(_group);
                                                        }
                                                    }
                                                }
                                            }
                                            break;
                                        case "EnvironmentalProfiles":
                                            _profiles.Import(_node as XmlElement);
                                            break;
                                        case "Components":
                                            _components.Import(_node as XmlElement);
                                            break;
                                    }
                                }
                                if (null != _reserved)
                                {
                                    WorkBreakdownStructureGroupViewModel _group = new WorkBreakdownStructureGroupViewModel();
                                    _group.Import(_reserved);
                                    _structures.Groups.Insert(_index, _group);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void OnCanExecuteConfigureCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteConfigureCommand(object sender, ExecutedRoutedEventArgs e)
        {
            ConfigurationPanel _panel = new ConfigurationPanel();
            CommonDialog _dialog = new CommonDialog(_panel);
            _dialog.Title = "설정";
            _dialog.Width = 400;
            _dialog.Height = 320;
            _dialog.ShowDialog();
        }

        private void OnCanExecuteExportCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            bool _export = false;
            if (null != mViewModel) if (true == mViewModel.Assessment) _export = null != mViewModel.Rates;
            e.CanExecute = _export;
        }
        private void OnExecuteExportCommand(object sender, ExecutedRoutedEventArgs e)
        {
            mViewModel.Rates.Export();
        }

        private void OnCanExecuteRefreshCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            bool _refresh = false;
            if (null != mViewModel) if (true == mViewModel.Assessment) _refresh = null != mViewModel.Rates;
            e.CanExecute = _refresh;
        }
        private void OnExecuteRefreshCommand(object sender, ExecutedRoutedEventArgs e)
        {
            AssessmentViewModel.GetInstance().Update();
        }

        private void OnCanExecuteProfileCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteProfileCommand(object sender, ExecutedRoutedEventArgs e)
        {
            ProgramProfilePanel _panel = new ProgramProfilePanel();
            CommonDialog _dialog = new CommonDialog(_panel);
            _dialog.Width = 500;
            _dialog.Height = 400;
            _dialog.Title = ProgramProfileViewModel.GetInstance().Product + " 정보";
            _dialog.ShowDialog();
        }
    }
}